package com.example.czxyseekbuy.web;

import com.example.czxyseekbuy.mapper.BuyMapper;
import com.yc.czxy.bean.Buy;
import com.yc.czxy.bean.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("buy")
public class BuyAction {

    @Resource
    BuyMapper buyMapper;

    /**
     * 添加求购
     * @return
     */
    @PostMapping("addBuy")
    public Result<Buy> addProduct(@RequestBody Buy buy) {
        System.out.println(buy.toString());
        int code = buyMapper.insert(buy);
        if (code > 0) {
            return new Result(code, "添加求购成功", buy);
        } else {
            return new Result(code, "添加求购失败", buy);
        }
    }

    /**
     * 根据用户用户id查询求购
     */
    @GetMapping("selectBuyByUid")
    public List<Buy> selectBuyByUid(Integer uid){
        return buyMapper.selectBuyByUid(uid);
    }

    /**
     * 根据bid删除求购
     * @return
     */
    @PostMapping("deleteBuyBybid")
    public Result<Buy> deleteBuyBybid(@RequestBody Buy buy) {
        int code = buyMapper.deleteById(buy.getBid());
        if (code > 0) {
            return new Result(code, "删除成功", buy);
        } else {
            return new Result(code, "删除失败", buy);
        }
    }

}
